/**
 * 小储云商城
 * 模板视图js调用类 cool
 * @type {{input_data: (function(*, *, *, *): string), data_class: (function(*): string), num: (function(): string), data_goods: (function(*): string)}}
 */
var template = {
    data_goods: function (data) { //商品列表模板
        if (data.quota != 0) {
            var quota = '' + data.quantity + '个';
        } else {
            var quota = '<font color=#EF5350>库存不足</font>'
        }
        if (data.image == '') {
            data.image = 'assets/cloud/image/error_img.png';
        }

        if (data.price == 0) {
            var buy = '<span class="buy" style="background-color: yellowgreen;color:#fff;position: absolute;    right: 0;">领取</span>'
        } else {
            var buy = '<span class="buy" style="color:#fff;position: absolute;    right: 0;">购买</span>';
        }

        var content = '<a class="fui-goods-item" title="' + quota + '" href="?mod=shop&gid=' + data.gid + '&cid=' + data.cid + '">\n' +
            '      <div class="image" style="background-image: url(' + data.image + '); background-size: 100% 100%">\n' +
            '      </div>\n' +
            '      <div class="detail">\n' +
            '          <div class="name" style="color: #000000;">\n' +
            '             ' + data.name + '<br>' + quota + '          </div>\n' +
            '          <div class="price" style="margin-top: 0.9rem;">\n' +
            '              <span class="text" style="color: #ff5555;"> <p class="minprice">￥' + (data.price - 0) + '</p> </span>\n' +
            '\n' +
            '              ' + buy + '          </div>\n' +
            '      </div>\n' +
            '  </a>';
        return content;
    },
    input_data: function (name, placeholder, id, addstr) { //商品输入框模板
        if (addstr != '') {
            var content = '<div class="layui-form-item">\n' +
                '      <label class="layui-form-label"  style="width: 100%;text-align: left;padding:0" id="var_c' + id + '" >' + name + '</label>\n' +
                '      <div class="layui-input-"  style="padding-right: 80px !important;" >\n' +
                '          <input type="text" onblur="checkInput(\'var_c' + id + '\',\'input_val_' + id + '\')" placeholder="' + placeholder + '"  id="input_val_' + id + '"    class="layui-input" />\n' +
                '      </div>' + addstr + '\n' +
                '  </div>';
        } else {
            var content = '<div class="layui-form-item">\n' +
                '      <label class="layui-form-label"  style="width: 100%;text-align: left;padding:0" id="var_c' + id + '" >' + name + '</label>\n' +
                '      <div class="layui-input-">\n' +
                '          <input type="text"  onblur="checkInput(\'var_c' + id + '\',\'input_val_' + id + '\')" placeholder="' + placeholder + '"  id="input_val_' + id + '"  class="layui-input">\n' +
                '      </div>\n' +
                '  </div>';
        }
        return content;
    },
    select_data: function (selectname, addstr, i) { //多选框构建
        var content = '<div class="layui-form-item">\n' +
            '      <label class="layui-form-label"  style="width: 100%;text-align: left;padding:0" id="var_c' + i + '">' + selectname + '</label>\n' +
            '      <div class="layui-input-">\n' +
            '          <select lay-verify="required"  id="input_val_' + i + '">\n' +
            '              <option value=""></option>' + addstr + '\n' +
            '          </select>\n' +
            '      </div>\n' +
            '  </div>';
        return content;
    },
    num: function () { //商品下单数量模板
        var content = '<div class="layui-form-item">\n' +
            '      <label class="layui-form-label" style="width: 100%;text-align: left;padding:0">下单份数</label>\n' +
            '      <div class="layui-input-">\n' +
            '          <input type="number" min="1" value="1"  placeholder="请填写商品下单份数！"  id="num"  class="layui-input">\n' +
            '      </div>\n' +
            '  </div>';
        return content;
    }, success: function (data) {
        if (data.data['CarriageTem'] != undefined && data.data['freight'] != "") { //启用运费模板
            if (data.data['freight'] != "-1") {
                CarriageTem.start(data.data['CarriageTem'], data.data['freight'], data.data['price']);
            }
        }

        var method = data.data.method; //商品状态

        $("#gootsp").text($("#gootsp").attr('value'));
        $(".pay_price").html(data.data['price'] + '元<br>');
        $(".quota").text(data.data['count_quota']);
        $(".points").text(data.data['points']);
        $("#level").html('当前级别：<font color="#48d1cc">' + data.data['level'] + '</font>');

        if (method.indexOf(1) != -1 || method.indexOf(2) != -1) { //是否关闭金额显示！
            $(".pay_prices").show(10);
        }

        if (method.indexOf(3) != -1) { //是否关闭积分显示！
            $(".pointss").show();
        }

        var picture = $("#gootsp").attr('data-picture');
        var coutent_img = '';
        if (picture == '' || picture == undefined) {
            coutent_img = '<div class="swiper-slide"><img onclick="template.image(\'' + $("#gootsp").attr('data-image') + '\')" src="' + $("#gootsp").attr('data-image') + '"  /></div>';
        } else {
            $("#swiper").show(100);
            array_img = picture.split('|');
            $.each(array_img, function (key, val) {
                coutent_img += '<div class="swiper-slide"><img onclick="template.image(\'' + val + '\')" src="' + val + '"  /></div>';
            });
        }
        $("#picture").html(coutent_img);

        var swiper = new Swiper('.swiper-container', {
            slidesPerView: 1,
            spaceBetween: 30,
            loop: true,
            pagination: {
                el: '.swiper-pagination',
                dynamicBullets: true,
            }
        });

        var level_arr = $("#gootsp").attr('data-level_arr');
        if (level_arr == '-1') {
            layer.tips('恭喜！您当前为最高等级密价！', '#level', {tips: [1, '#651FFF'], zIndex: 1});
            $("#level").attr('onclick', 'layer.msg(\'恭喜！您当前为最高等级密价！\')');
        } else {
            layer.tips('此商品有更多优惠哦！<br>点击查看获取！', '#level', {tips: [1, '#6200EA'], zIndex: 1});

            $("#level").attr('onclick', 'layer.alert(\'' + unescape(level_arr) + '\',{title:\'密价等级表\',btn:[\'确定\',\'升级密价\'],shade: [0.8, \'#000\'],shadeClose:true,btn2:function(layero,index){window.open(\'/user/grade.php\')}})');
        }

        $(".thumb img").attr('src', data.data.image);
        $("#cart_sum").html(' (' + data.data.cart_count + ')');
    },
    image: function (img) {
        swal({
            title: false,
            text: false,
            imageUrl: img,
            imageWidth: 300,
            imageHeight: 300,
            animation: false,
            customClass: 'jello animated',
            showConfirmButton: false,
        });
    },
    query_tem: function (data) { //订单模板
        $("#order_all,#order_a,#order_b,#order_c,#order_d").html('');
        var content_error = '<div class="layui-col-xs12 layui-col-sm6"><div class="layui-card qt-card">\n' +
            '              <div class="layui-card-header">\n' +
            '               <p style="width: 70%" class="layui-elip">没有查询到相关订单</p>\n' +
            '                       </div>\n' +
            '                        <div class="layui-card-body">\n' +
            '                       <div class="layui-row layui-col-space10">\n' +
            '                            <div class="layui-col-xs4" style="text-align: center">\n' +
            '                                <img src="' + logo + '">\n' +
            '                            </div><div class="layui-col-xs8" style="text-align: center">\n' +
            '                                登陆后台可永久保存订单哦<hr><a href="user">进入后台</a>\n' +
            '                            </div>\n' +
            '         </div>\n' +
            '    </div>\n' +
            '</div></div><hr>';

        if (data.code == 1) {
            var content = ''; //全部订单
            var content_a = ''; //待提交
            $.each(data.data, function (key, val) {

                switch (val['state']) {
                    case '1': //成功
                        var state = '<font color="#43A047">已完成</font>';
                        break;
                    case '2': //待处理
                        var state = '<font color="#651FFF">待处理</font>';
                        break;
                    case '3': //异常
                        var state = '<font color="#FF1744">异常中</font>';
                        break;
                    case '4': //正在处理
                        var state = '<font color="#F57C00">正在处理</font>';
                        break;
                    case '5': //已退款
                        var state = '<font color="#424242">已退款</font>';
                        break;
                    case '6': //售后中
                        var state = '<font color="#8b0000">售后中</font>';
                        break;
                    case '7': //已评价结单
                        var state = '<font color="#8fbc8f">已评价</font>';
                        break;
                    default:
                        var state = '<font color="#607D8B">未知状态</font>';
                        break;
                }

                if (val.payment == '积分兑换') {
                    val.price = (val.price - 0).toFixed(0) + '积分';
                } else if (val.payment == '免费领取') {
                    val.price = '<font color="#2e8b57">免费商品</font>';
                } else {
                    val.price = (val.price - 0).toFixed(2) + '元';
                }

                btne = '';

                if (val.token !== "0") {
                    msg_token = (val.token).join('<br>');
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="layer.alert(\'卡密信息为：<br>' + msg_token + '\',{title:\'您购买的卡密信息\'})">\n' +
                        '                             卡密\n' +
                        '                      </button>';
                }

                if (val.logistics != "-1") {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="layer.alert(\'快递单号信息：<br>' + val.logistics + '\<hr>查询快递方法：复制快递单号，手机打开菜鸟包裹查询即可！\',{title:\'您的快递单号\'})">\n' +
                        '                             物流\n' +
                        '                      </button>';
                }

                if (val['state'] != 5 && val['state'] != 7) {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.tickets(' + val.id + ')">\n' +
                        '                             售后\n' +
                        '                      </button>';
                }

                if (val['take'] == 2 && val.state != 7) {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_mark(' + val.id + ')"  >\n' +
                        '                             评价\n' +
                        '                      </button>';
                }

                if (val['take'] == 1 && val['state'] != 5 && val['state'] != 7) {
                    btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_take(' + val.id + ')" >\n' +
                        '                             确认收货\n' +
                        '                      </button>';
                }

                btne += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_delete(' + val.id + ')">\n' +
                    '                             删除\n' +
                    '                      </button>';

                content += '<div class="layui-card qt-card">\n' +
                    '              <div class="layui-card-header">\n' +
                    '               <p style="width: 70%" class="layui-elip">' + val.name + '</p>\n' +
                    '                    <span class="layui-layout-right layui-elip"\n' +
                    '                           style="width:30%;text-align: right;margin-right: 0.5em">\n' +
                    '                          ' + state + '\n' +
                    '                    </span>\n' +
                    '                       </div>\n' +
                    '                        <div class="layui-card-body">\n' +
                    '                       <div class="layui-row layui-col-space10">\n' +
                    '                            <div class="layui-col-xs4">\n' +
                    '                                <a href="/?gid=' + val.gid + '&cid=' + val.cid + '"><img src="' + val.image + '"/></a>\n' +
                    '                            </div>\n' +
                    '                       <div class="layui-col-xs8"\n' +
                    '                            style="font-size: 0.8em;color:black;font-family: \'微软雅黑\'">\n' +
                    '                            下单时间：' + val.addtitm + '<br>\n' +
                    '                            商品总价：' + val.price + '<br>\n' +
                    '                            购买方式：' + val.payment + '\n' +
                    '                      </div>\n' +
                    '                  <div style="width: 100%;text-align: right">\n' +
                    '                       <button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="merchandise.query_data(' + val.id + ')">\n' +
                    '                             详情\n' +
                    '                      </button>' + btne + '\n' +
                    '               </div>\n' +
                    '         </div>\n' +
                    '    </div>\n' +
                    '</div>';
            });


            $.each(data.data2, function (key, val) {
                btns = '';
                if (val.payment == '积分兑换') {
                    val.price = (val.price - 0).toFixed(0) + '积分';
                } else if (val.payment == '免费领取') {
                    val.price = '<font color="#2e8b57">免费商品</font>';
                } else {
                    val.price = (val.price - 0).toFixed(2) + '元';
                }

                switch (val['type']) {
                    case '2':
                        var state = '<font color="#20b2aa">待提交服务器</font>';
                        btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.SubmitOrder(' + val.id + ')">\n' +
                            '                             手动提交\n' +
                            '                      </button>';
                        break;
                    case '3':
                        var state = '<font color="#1e90ff">待付款</font>';
                        btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.OrderPay(' + val.id + ')">\n' +
                            '                             点击付款\n' +
                            '                      </button>';
                        btns += '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.OrderDel(' + val.id + ')">\n' +
                            '                             点击删除\n' +
                            '                      </button>';
                        break;
                    default:
                        var state = '<font color="#ff4500">已失效</font>';
                        btns = '<button class="layui-btn qt-btn layui-btn-sm layui-btn-primary" onclick="template.OrderDel(' + val.id + ')">\n' +
                            '                             点击删除\n' +
                            '                      </button>';
                        break;
                }

                content_a += '<div class="layui-card qt-card" id="order_' + val.id + '">\n' +
                    '              <div class="layui-card-header">\n' +
                    '               <p style="width: 70%" class="layui-elip">' + val.name + '</p>\n' +
                    '                    <span class="layui-layout-right layui-elip"\n' +
                    '                           style="width:30%;text-align: right;margin-right: 0.5em">\n' +
                    '                          ' + state + '\n' +
                    '                    </span>\n' +
                    '                       </div>\n' +
                    '                        <div class="layui-card-body">\n' +
                    '                       <div class="layui-row layui-col-space10">\n' +
                    '                            <div class="layui-col-xs4">\n' +
                    '                                <a href="/?gid=' + val.gid + '&cid=' + val.cid + '"><img src="' + val.image + '"/></a>\n' +
                    '                            </div>\n' +
                    '                       <div class="layui-col-xs8"\n' +
                    '                            style="font-size: 0.8em;color:black;font-family: \'微软雅黑\'">\n' +
                    '                            商品总价：' + val.price + '<br>\n' +
                    '                            购买方式：' + val.payment + '<br>\n' +
                    '                            创建时间：' + val.addtime + '<br>\n' +
                    '                            提交时间：' + (val.endtime == undefined ? '尚未付款,无法提交' : val.endtime) + '<br>\n' +
                    '                            订单备注：' + val.remark + '<br>\n' +
                    '                      </div>\n' +
                    '                  <div style="width: 100%;text-align: right">\n' + btns +
                    '               </div>\n' +
                    '         </div>\n' +
                    '    </div>\n' +
                    '</div>';
            });


            if (content == '') {
                content = content_error;
            }

            if (content_a == '') {
                content_a = content_error;
            }

            $("#order_all").html(content);
            $("#order_a").html(content_a);
            content = '';
        } else {
            $("#order_all,#order_a,#order_b").html(content_error);
        }

        $.ajax({
            type: "post",
            url: "/api.php?act=OrdersTesting",
            dataType: "json",
        });
        $.ajax({
            type: "post",
            url: "/api.php?act=SubmitOrder",
            data: {num: 2},
            dataType: "json",
        });
    },
    OrderDel: function (id) {
        layer.load(2, {time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=OrderDel",
            data: {id: id},
            dataType: "json",
            success: function (data) {
                if (data.code == 1) {
                    $("#order_" + id).hide(100);
                    layer.msg(data.msg, {icon: 1});
                    OrderManagement.OrderQuery('OrderAll');
                } else layer.msg(data.msg, {icon: 2});
            },
            error: function () {
                layer.closeAll();
                layer.alert('删除失败！');
            }
        });
    },
    SubmitOrder: function (id) {
        layer.load(2, {time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=SubmitOrder",
            data: {id: id},
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                if (data.code == 1) {
                    layer.msg(data.msg, {icon: 1});
                    OrderManagement.OrderQuery('OrderAll');
                } else layer.msg(data.msg, {icon: 2});
            },
            error: function () {
                layer.closeAll();
                layer.alert('提交失败！');
            }
        });
    },
    OrderPay: function (id) {
        layer.load(2, {time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=OrderPay",
            data: {id: id},
            dataType: "json",
            success: function (data) {
                layer.closeAll();
                if (data.code == 1) {
                    var inst = layer.msg(data.msg, {
                        btn: ['确定'], time: 9999999, btn1: function (layero, index) {
                            layer.close(inst);
                            OrderManagement.OrderQuery('OrderAll');
                        }
                    })
                } else if (data.code == 2) {
                    location.href = data.url;
                    setTimeout(function () {
                        layer.alert('<font size="4" color="#2e8b57">若未跳转付款界面,请点击下方付款按钮！</font>', {
                            title: '温馨提示!',
                            btn: ['付款'], time: 9999999, btn1: function (layero, index) {
                                location.href = data.url;
                            }
                        })
                    }, 500);
                } else {
                    layer.alert(data.msg, {icon: 2, title: '温馨提示'});
                }
            },
            error: function () {
                layer.closeAll();
                layer.alert('购买失败！');
            }
        });
    },
    SharePoster: function (gid) {
        layer.msg('正在生成商品分享海报...', {icon: 16, time: 9999999});
        $.ajax({
            type: "post",
            url: "/ajax.php?act=SharePoster",
            data: {gid: gid},
            dataType: "json",
            success: function (data) {
                if (data.code == 1) {
                    layer.alert('<img src="' + data.src + '" width=300 heigth=450 />', {
                        area: ['340px', '490px'],
                        title: false,
                        btn: false, shade: [0.8, '#000'],
                        shadeClose: true,
                    })
                } else {
                    layer.msg(data.msg, {icon: 2});
                }
            },
            error: function () {
                layer.alert('生成失败！');
            }
        });
    }

};

if ($_GET['cid'] != undefined && $_GET['gid'] == undefined || $_GET['name'] != undefined) {
    merchandise.gid_data($_GET['cid'], $_GET['name']);
} else if ($_GET['cid'] != undefined && $_GET['gid'] != undefined) {
    /**
     * 调用商品下单界面模块
     */
    merchandise.shop_data($_GET['gid']);
} else {
    merchandise.gid_data(-1);
}